<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Go语言函数 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/76.61078c10.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/100.05bb269d.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/102.c7d7ed9c.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/66.cd973084.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/79.2c7d6b79.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/91.b8d1b7ea.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/95.1b379a5b.js"><link rel="prefetch" href="/assets/js/96.9fd67185.js"><link rel="prefetch" href="/assets/js/97.c5a6a5a1.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go教程</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/learn-go/10 从零搭建Go语言开发环境.html" class="sidebar-link">从零搭建Go语言开发环境</a></li><li><a href="/go/learn-go/11 第一个Go程序.html" class="sidebar-link">第一个Go程序Hello World</a></li><li><a href="/go/learn-go/12 Go 语言数据类型.html" class="sidebar-link">Go语言数据类型</a></li><li><a href="/go/learn-go/13 Go语言变量.html" class="sidebar-link">Go语言变量</a></li><li><a href="/go/learn-go/14 Go语言常量.html" class="sidebar-link">Go语言常量</a></li><li><a href="/go/learn-go/15 Go 语言运算符.html" class="sidebar-link">Go语言运算符</a></li><li><a href="/go/learn-go/16 Go语言条件语句.html" class="sidebar-link">Go语言条件语句</a></li><li><a href="/go/learn-go/17 Go语言的循环语句.html" class="sidebar-link">Go语言的循环语句</a></li><li><a href="/go/learn-go/18 Go 语言数组.html" class="sidebar-link">Go语言数组</a></li><li><a href="/go/learn-go/19 Go语言切片.html" class="sidebar-link">Go语言基础之切片</a></li><li><a href="/go/learn-go/20 Go语言map.html" class="sidebar-link">Go语言基础之map</a></li><li><a href="/go/learn-go/21 Go语言指针.html" class="sidebar-link">Go语言指针</a></li><li><a href="/go/learn-go/22 Go语言函数.html" class="active sidebar-link">Go语言函数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_1-函数定义" class="sidebar-link">1 函数定义</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_2-函数的参数说明" class="sidebar-link">2 函数的参数说明</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_2-1-类型简写" class="sidebar-link">2.1 类型简写</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_2-2-可变参数" class="sidebar-link">2.2 可变参数</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_2-3-多返回值" class="sidebar-link">2.3 多返回值</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_2-4-返回值命名" class="sidebar-link">2.4 返回值命名</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_3-基本函数举例" class="sidebar-link">3 基本函数举例</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_4-函数类型与变量" class="sidebar-link">4 函数类型与变量</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_4-1-定义函数类型" class="sidebar-link">4.1 定义函数类型</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_4-2-函数类型变量" class="sidebar-link">4.2 函数类型变量</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_5-高阶函数" class="sidebar-link">5 高阶函数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_5-1-函数作为参数" class="sidebar-link">5.1 函数作为参数</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_5-2-函数作为返回值" class="sidebar-link">5.2 函数作为返回值</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_5-3-递归函数" class="sidebar-link">5.3 递归函数</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_6-匿名函数和闭包" class="sidebar-link">6 匿名函数和闭包</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_6-1-匿名函数" class="sidebar-link">6.1 匿名函数</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_6-2-闭包" class="sidebar-link">6.2 闭包</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_7-defer语句" class="sidebar-link">7 defer语句</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_7-1-defer执行时机" class="sidebar-link">7.1 defer执行时机</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_7-2-defer经典案例" class="sidebar-link">7.2 defer经典案例</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_8-内置函数介绍" class="sidebar-link">8 内置函数介绍</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_8-1-panic-recover" class="sidebar-link">8.1 panic/recover</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_8-2-示例代码一" class="sidebar-link">8.2 示例代码一</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_8-3-示例代码二" class="sidebar-link">8.3 示例代码二</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/22 Go语言函数.html#_8-4-recover-panic注意点" class="sidebar-link">8.4 recover/panic注意点</a></li></ul></li></ul></li><li><a href="/go/learn-go/23  fmt.Printf总结.html" class="sidebar-link">fmt.Printf总结</a></li><li><a href="/go/learn-go/24 Go语言常用方法总结.html" class="sidebar-link">Go常用方法总结</a></li><li><a href="/go/learn-go/25 Go语言结构体.html" class="sidebar-link">Go语言结构体</a></li><li><a href="/go/learn-go/26 Go语言方法.html" class="sidebar-link">Go语言方法</a></li><li><a href="/go/learn-go/27 Go语言接口.html" class="sidebar-link">Go语言接口</a></li><li><a href="/go/learn-go/28 Go语言并发编程.html" class="sidebar-link">Go语言并发编程</a></li><li><a href="/go/learn-go/29 Go语言文件操作.html" class="sidebar-link">Go语言文件操作</a></li><li><a href="/go/learn-go/30 Go语言单元测试.html" class="sidebar-link">Go语言单元测试</a></li><li><a href="/go/learn-go/31 Go语言之http.html" class="sidebar-link">Go语言之http</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go Web框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go工具中间件</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="go语言函数"><a href="#go语言函数" class="header-anchor">#</a> Go语言函数</h1> <h2 id="_1-函数定义"><a href="#_1-函数定义" class="header-anchor">#</a> 1 函数定义</h2> <p><strong>Go语言中定义函数使用<code>func</code>关键字，函数的参数和返回值都是可选的,具体格式如下：</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> 函数名<span class="token punctuation">(</span>参数<span class="token punctuation">)</span><span class="token punctuation">(</span>返回值<span class="token punctuation">)</span><span class="token punctuation">{</span>
    函数体
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>函数名：由字母、数字、下划线组成。但函数名的第一个字母不能是数字。在同一个包内，函数名也称不能重名（包的概念详见后文）。</li> <li>参数：参数由参数变量和参数变量的类型组成，多个参数之间使用,分隔。</li> <li>返回值：返回值由返回值变量和其变量类型组成，也可以只写返回值的类型，多个返回值必须用()包裹，并用,分隔。</li> <li>函数体：实现指定功能的代码块。</li></ul> <h2 id="_2-函数的参数说明"><a href="#_2-函数的参数说明" class="header-anchor">#</a> 2 函数的参数说明</h2> <h3 id="_2-1-类型简写"><a href="#_2-1-类型简写" class="header-anchor">#</a> 2.1 类型简写</h3> <p><strong>函数的参数中如果相邻变量的类型相同，则可以省略类型，例如</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">intSum</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> x <span class="token operator">+</span> y
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><blockquote><p>上面的代码中，intSum函数有两个参数，这两个参数的类型均为int，因此可以省略x的类型，因为y后面有类型说明，x参数也是该类型。</p></blockquote> <h3 id="_2-2-可变参数"><a href="#_2-2-可变参数" class="header-anchor">#</a> 2.2 可变参数</h3> <p><strong>可变参数是指函数的参数数量不固定。Go语言中的可变参数通过在参数名后加<code>...</code>来标识。</strong></p> <p><strong>注意：可变参数通常要作为函数的最后一个参数。</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">intSum2</span><span class="token punctuation">(</span>x <span class="token operator">...</span><span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token comment">//x是一个切片</span>
	sum <span class="token operator">:=</span> <span class="token number">0</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> v <span class="token operator">:=</span> <span class="token keyword">range</span> x <span class="token punctuation">{</span>
		sum <span class="token operator">=</span> sum <span class="token operator">+</span> v
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> sum
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_2-3-多返回值"><a href="#_2-3-多返回值" class="header-anchor">#</a> 2.3 多返回值</h3> <p><strong>Go语言中函数支持多返回值，函数如果有多个返回值时必须用()将所有返回值包裹起来。</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">calc</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	sum <span class="token operator">:=</span> x <span class="token operator">+</span> y
	sub <span class="token operator">:=</span> x <span class="token operator">-</span> y
	<span class="token keyword">return</span> sum<span class="token punctuation">,</span> sub
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_2-4-返回值命名"><a href="#_2-4-返回值命名" class="header-anchor">#</a> 2.4 返回值命名</h3> <p><strong>函数定义时可以给返回值命名，并在函数体中直接使用这些变量，最后通过<code>return</code>关键字返回。</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">calc</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>sum<span class="token punctuation">,</span> sub <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	sum <span class="token operator">=</span> x <span class="token operator">+</span> y
	sub <span class="token operator">=</span> x <span class="token operator">-</span> y
	<span class="token keyword">return</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="_3-基本函数举例"><a href="#_3-基本函数举例" class="header-anchor">#</a> 3 基本函数举例</h2> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">sub</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;sub()求两个参数的差值(%d-%d)=%d\n&quot;</span><span class="token punctuation">,</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span><span class="token punctuation">(</span>x<span class="token operator">-</span>y<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> x <span class="token operator">-</span> y
<span class="token punctuation">}</span>


<span class="token keyword">func</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;print()没有参数没有返回值的函数……&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">sum2</span><span class="token punctuation">(</span>x <span class="token builtin">int32</span><span class="token punctuation">,</span>y <span class="token builtin">int32</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>x<span class="token operator">+</span>y<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;sum2()有参数没有返回值的函数&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">sum3</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>ret <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;sum3() 没有参数有返回值的函数&quot;</span><span class="token punctuation">)</span>
	x<span class="token operator">:=</span><span class="token number">12</span>
	y<span class="token operator">:=</span><span class="token number">24</span>
	ret<span class="token operator">=</span>x<span class="token operator">+</span>y
	<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">sum</span><span class="token punctuation">(</span>x <span class="token builtin">int32</span><span class="token punctuation">,</span>y <span class="token builtin">int32</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>ret <span class="token builtin">int32</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;sum()有参数有返回值的函数&quot;</span><span class="token punctuation">)</span>
	ret<span class="token operator">=</span>x<span class="token operator">+</span>y
	<span class="token keyword">return</span> ret
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">sum4</span><span class="token punctuation">(</span>x<span class="token operator">...</span> <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>ret <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;sum4()多个int变量求和的方法&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> v <span class="token operator">:=</span> <span class="token keyword">range</span> x<span class="token punctuation">{</span>
		ret <span class="token operator">=</span> ret <span class="token operator">+</span> v
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//函数的调用 ，求两个int类型的差值</span>
	<span class="token function">sub</span><span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">)</span>
	<span class="token comment">//调用多个参数求和的函数</span>
	ret <span class="token operator">:=</span> <span class="token function">sum4</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
	ret <span class="token operator">=</span> <span class="token function">sum4</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token number">40</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>

<span class="token keyword">func</span> <span class="token function">f1</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">//Go语言中函数的传递的都是值（相当于Ctrl+C Ctrl+V）</span>
	a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">100</span> <span class="token comment">//这个地方相当于修改的副本数组的值，原来数组的值不变</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;传入数组a要修改的元素的内存地址是%v\n&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//0xc0000103a8</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	x<span class="token operator">:=</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token comment">//[1 2 3]</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;x本身数组的内存地址是%v\n&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>x<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">//0xc000010348</span>
	<span class="token function">f1</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token comment">//调用函数</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token comment">//[1 2 3]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="_4-函数类型与变量"><a href="#_4-函数类型与变量" class="header-anchor">#</a> 4 函数类型与变量</h2> <h3 id="_4-1-定义函数类型"><a href="#_4-1-定义函数类型" class="header-anchor">#</a> 4.1 定义函数类型</h3> <p><strong>我们可以使用type关键字来定义一个函数类型，具体格式如下</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> calculation <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><blockquote><p>上面语句定义了一个calculation类型，它是一种函数类型，这种函数接收两个int类型的参数并且返回一个int类型的返回值。</p></blockquote> <p><strong>凡是满足这个条件的函数都是calculation类型的函数，例如下面的add和sub是calculation类型</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> x <span class="token operator">+</span> y
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">sub</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> x <span class="token operator">-</span> y
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><strong>add和sub都能赋值给calculation类型的变量。</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> c calculation
c <span class="token operator">=</span> add
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_4-2-函数类型变量"><a href="#_4-2-函数类型变量" class="header-anchor">#</a> 4.2 函数类型变量</h3> <p><strong>我们可以声明函数类型的变量并且为该变量赋值</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
<span class="token punctuation">)</span>
<span class="token keyword">type</span> calculation <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span>

<span class="token keyword">func</span> <span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span><span class="token punctuation">{</span>
	<span class="token keyword">return</span> x<span class="token operator">+</span>y
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">sub</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> x <span class="token operator">-</span> y
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">var</span> c calculation               <span class="token comment">// 声明一个calculation类型的变量c</span>
	c <span class="token operator">=</span> add                         <span class="token comment">// 把add赋值给c</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;type of c:%T\n&quot;</span><span class="token punctuation">,</span> c<span class="token punctuation">)</span> <span class="token comment">// type of c:main.calculation</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">c</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>            <span class="token comment">// 像调用add(5,2)一样调用c</span>

	f <span class="token operator">:=</span> sub                   <span class="token comment">// 将函数sub赋值给变量f1</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;type of f:%T\n&quot;</span><span class="token punctuation">,</span> f<span class="token punctuation">)</span> <span class="token comment">// type of f:func(int, int) int</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">f</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">)</span>          <span class="token comment">// 像调用sub(10,20)一样调用f</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><blockquote><p>一个方法就是一个包含了接收者的函数。接收者可以是命名类型或者结构体类型的一个值或者是一个指针。所有给定类型的方法属于该类型的方法集。</p></blockquote> <h2 id="_5-高阶函数"><a href="#_5-高阶函数" class="header-anchor">#</a> 5 高阶函数</h2> <p>高阶函数分为函数作为参数和函数作为返回值两部分。</p> <h3 id="_5-1-函数作为参数"><a href="#_5-1-函数作为参数" class="header-anchor">#</a> 5.1 函数作为参数</h3> <p>函数可以作为参数：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> x <span class="token operator">+</span> y
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">calc</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">,</span> op <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token function">op</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	ret2 <span class="token operator">:=</span> <span class="token function">calc</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> add<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>ret2<span class="token punctuation">)</span> <span class="token comment">//30</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="_5-2-函数作为返回值"><a href="#_5-2-函数作为返回值" class="header-anchor">#</a> 5.2 函数作为返回值</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">do</span><span class="token punctuation">(</span>s <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">switch</span> s <span class="token punctuation">{</span>
	<span class="token keyword">case</span> <span class="token string">&quot;+&quot;</span><span class="token punctuation">:</span>
		<span class="token keyword">return</span> add<span class="token punctuation">,</span> <span class="token boolean">nil</span>
	<span class="token keyword">case</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">:</span>
		<span class="token keyword">return</span> sub<span class="token punctuation">,</span> <span class="token boolean">nil</span>
	<span class="token keyword">default</span><span class="token punctuation">:</span>
		err <span class="token operator">:=</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;无法识别的操作符&quot;</span><span class="token punctuation">)</span>
		<span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> err
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="_5-3-递归函数"><a href="#_5-3-递归函数" class="header-anchor">#</a> 5.3 递归函数</h3> <div class="custom-block tip"><p class="custom-block-title">概述</p> <ul><li>递归：可以简单理解为自己调自己</li> <li>优点：逻辑比循环更清晰</li> <li>注意：使用递归函数要注意防止栈溢出。在计算机中，函数调用是通过栈（stack）这种数据
结构实现的。每当进入一个函数调用，栈就会加一层栈。每当函数返回，栈就会减少一层。
由于栈的大小不是无限的，所以，递归调用的次数过多，会导致栈溢出。</li></ul></div> <p><strong>递归举例---&gt;阶乘</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//用递归实现阶乘计算</span>
<span class="token keyword">func</span> <span class="token function">factorial</span><span class="token punctuation">(</span>n <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>result <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">if</span> n <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token number">1</span>
	<span class="token punctuation">}</span>
	result <span class="token operator">=</span> n <span class="token operator">*</span> <span class="token function">factorial</span><span class="token punctuation">(</span>n<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span>
<span class="token punctuation">}</span>

<span class="token comment">//用for循环实现阶乘计算</span>
<span class="token keyword">func</span> <span class="token function">getMultiple</span><span class="token punctuation">(</span>num <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>result <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	result <span class="token operator">=</span> <span class="token number">1</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> num<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		result <span class="token operator">*=</span> i
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p><strong>递归举例---&gt;上台阶</strong></p> <div class="custom-block tip"><p class="custom-block-title">编程题</p> <ul><li>编程题：有n个台阶，一次只能上1步或者2步，共有多少种走法？</li> <li>考察的知识点：</li> <li>递归和循环迭代</li></ul></div> <p>思路整理</p> <table><thead><tr><th>n 的值</th> <th>走法</th> <th>算式</th></tr></thead> <tbody><tr><td>1</td> <td>只能一次1步</td> <td>f(1) = 1</td></tr> <tr><td>2</td> <td>（1）一次走1步<br>（2）直接走2步</td> <td>f(2) = 2</td></tr> <tr><td>3</td> <td>（1）先到达f(1)的情况，再从f(1)直接跨2步<br>（2）先到达f(2)的情况，再从f(2)直接跨1步</td> <td>f(3) = f(2) + f(1) = 3</td></tr> <tr><td>4</td> <td>（ 1）先到达f(2)的情况，再从f(2)直接跨2步<br>（2）先到达f(3)的情况，再从f(3)直接跨1步</td> <td>f(4) = f(3) + f(2) = 5</td></tr> <tr><td>...</td> <td>...</td> <td>...</td></tr> <tr><td>n = x</td> <td>（1）先到达f(n-2)的情况，再从f(n-2)直接跨2步<br>（2）先到达f(n-1)的情况，再从f(n-1)直接跨1步</td> <td>f(x) = f(x - 2) + f(x- 1)</td></tr></tbody></table> <p>递归方式的示例代码：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;strconv&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">diGui</span><span class="token punctuation">(</span>n <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	<span class="token keyword">if</span> n <span class="token operator">&lt;</span> <span class="token number">1</span> <span class="token punctuation">{</span>
	  <span class="token function">panic</span><span class="token punctuation">(</span>strconv<span class="token punctuation">.</span><span class="token function">Itoa</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;不能小于1&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">if</span> n <span class="token operator">==</span> <span class="token number">1</span> <span class="token operator">||</span> n <span class="token operator">==</span> <span class="token number">2</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> n
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token function">diGui</span><span class="token punctuation">(</span>n<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">diGui</span><span class="token punctuation">(</span>n<span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	num <span class="token operator">:=</span> <span class="token function">diGui</span><span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;共有%d种走法\n&quot;</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h2 id="_6-匿名函数和闭包"><a href="#_6-匿名函数和闭包" class="header-anchor">#</a> 6 匿名函数和闭包</h2> <h3 id="_6-1-匿名函数"><a href="#_6-1-匿名函数" class="header-anchor">#</a> 6.1 匿名函数</h3> <blockquote><p>函数当然还可以作为返回值，但是在Go语言中函数内部不能再像之前那样定义函数了，只能定义匿名函数。匿名函数就是没有函数名的函数，匿名函数多用于实现回调函数和闭包。
匿名函数的定义格式如下：</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span><span class="token punctuation">(</span>参数<span class="token punctuation">)</span><span class="token punctuation">(</span>返回值<span class="token punctuation">)</span><span class="token punctuation">{</span>
    函数体
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><blockquote><p>匿名函数因为没有函数名，所以没办法像普通函数那样调用，所以匿名函数需要保存到某个变量或者作为立即执行函数:</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// 将匿名函数保存到变量</span>
	add <span class="token operator">:=</span> <span class="token keyword">func</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>x <span class="token operator">+</span> y<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token function">add</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span> <span class="token comment">// 通过变量调用匿名函数</span>

	<span class="token comment">//自执行函数：匿名函数定义完加()直接执行</span>
	<span class="token keyword">func</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>x <span class="token operator">+</span> y<span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="_6-2-闭包"><a href="#_6-2-闭包" class="header-anchor">#</a> 6.2 闭包</h3> <blockquote><p>闭包是引用了自由变量的函数,被引用的自由变量和函数一同存在,即使已经离开了自由变量的环境也不会被释放或者删除,在闭包中可以继续使用这个自由变量。说一下我自己对闭包的理解，闭包就是函数返回一个匿名函数。简单的说:<code>函数</code> + <code>引用环境</code> = <code>闭包</code></p></blockquote> <p>同一个函数与不同引用环境组合，可以形成不同的实例，如下图所示
<a data-fancybox="" title="闭包与函数引用" href="/img/goImage/bibao.png"><img src="/img/goImage/bibao.png" alt="闭包与函数引用"></a></p> <h4 id="_6-2-1-举例一"><a href="#_6-2-1-举例一" class="header-anchor">#</a> 6.2.1 举例一</h4> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>

<span class="token keyword">func</span> <span class="token function">returnNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
    q <span class="token operator">:=</span> <span class="token function">returnNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    a<span class="token punctuation">,</span> b <span class="token operator">:=</span> <span class="token function">q</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token comment">//0, 1</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h4 id="_6-2-2-闭包的最佳实践"><a href="#_6-2-2-闭包的最佳实践" class="header-anchor">#</a> 6.2.2 闭包的最佳实践</h4> <div class="custom-block tip"><p class="custom-block-title">编写程序具体要求</p> <ul><li>1、编写一个函数<code>makeSuffix(suffix string)</code>,可以接收一个文件后缀名(比如<code>.jpg</code>),并返回一个闭包</li> <li>2、调用闭包,可以传入一个文件名,如果该文件名没有指定的后缀(比如<code>.jpg</code>),则返回文件名<code>.jpg</code>,如果有<code>.jpg</code>后缀，则返回原文件名</li> <li>3、<code>strings.HasSuffix</code>,该函数可以判断某个字符串是否有指定的后缀</li></ul></div> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;strings&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">makeSuffix</span><span class="token punctuation">(</span>suffix <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token keyword">func</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
		<span class="token comment">//如果name没有指定的后缀，则加上，否则就返回原来的名字</span>
		<span class="token keyword">if</span> <span class="token operator">!</span>strings<span class="token punctuation">.</span><span class="token function">HasSuffix</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> suffix<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">return</span> name <span class="token operator">+</span> suffix
		<span class="token punctuation">}</span>
		<span class="token keyword">return</span> name
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	f2 <span class="token operator">:=</span> <span class="token function">makeSuffix</span><span class="token punctuation">(</span><span class="token string">&quot;.jpg&quot;</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;文件名处理后=&quot;</span><span class="token punctuation">,</span> <span class="token function">f2</span><span class="token punctuation">(</span><span class="token string">&quot;winter&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">//文件名处理后= winter.jpg</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;文件名处理后=&quot;</span><span class="token punctuation">,</span> <span class="token function">f2</span><span class="token punctuation">(</span><span class="token string">&quot;bird.jpg&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//文件名处理后= bird.jpg</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="_7-defer语句"><a href="#_7-defer语句" class="header-anchor">#</a> 7 defer语句</h2> <blockquote><p>Go语言中的defer语句会将其后面跟随的语句进行延迟处理。在defer归属的函数即将返回时，将延迟处理的语句按defer定义的逆序进行执行，也就是说，先被defer的语句最后被执行，最后被defer的语句，最先被执行。</p></blockquote> <div class="custom-block tip"><p class="custom-block-title">defer延迟调用：</p> <ul><li>1、确保调用在函数结束时发生；</li> <li>2、defer列表为先进后出；</li> <li>3、通常在Open/Close  Lock/Unlock中使用。</li></ul></div> <div class="custom-block danger"><p class="custom-block-title">defer的应用场景</p> <ul><li>事务的提交或回滚</li> <li>用于锁的关闭，连接的延迟关闭等，通常在函数的结束时调用</li> <li>资源清理、文件关闭、数据库连接关闭、解锁及记录时间等</li> <li>另外defer确实有一定的开销，拒绝滥用</li></ul></div> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;start&quot;</span><span class="token punctuation">)</span> 
	<span class="token keyword">defer</span> fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token keyword">defer</span> fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
	<span class="token keyword">defer</span> fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;end&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>输出</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>start
end
<span class="token number">3</span>
<span class="token number">2</span>
<span class="token number">1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_7-1-defer执行时机"><a href="#_7-1-defer执行时机" class="header-anchor">#</a> 7.1 defer执行时机</h3> <blockquote><p>在Go语言的函数中return语句在底层并不是原子操作，它分为给返回值赋值和RET指令两步。而defer语句执行的时机就在返回值赋值操作后，RET指令执行前。具体如下图所示：
<a data-fancybox="" title="defer执行时机" href="/img/goImage/defer.png"><img src="/img/goImage/defer.png" alt="defer执行时机"></a></p></blockquote> <h3 id="_7-2-defer经典案例"><a href="#_7-2-defer经典案例" class="header-anchor">#</a> 7.2 defer经典案例</h3> <p>注意：</p> <ul><li>go语言中函放的 return不是原子操作,在底层是分为两步来执行</li> <li>第一步:返回值赋值</li> <li>第二步:真正的RET返回</li> <li>函数中如果存在 defer,那么 defer执行的时机是在第一歩和第二步之间</li></ul> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">f1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
<span class="token comment">//1 返回值赋值  返回值=x=5</span>
<span class="token comment">// 2 执行defer 修改x的值而不是返回值的值</span>
<span class="token comment">// 3 真正的返回</span>
	x <span class="token operator">:=</span> <span class="token number">5</span>
	<span class="token keyword">defer</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		x<span class="token operator">++</span>
	<span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> x
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">f2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>x <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//1 返回值赋值  返回值为x 此时x=5</span>
<span class="token comment">// 2 执行defer 修改x的值 此时x=6</span>
<span class="token comment">// 3 真正的返回x 所以结果为6</span>
	<span class="token keyword">defer</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		x<span class="token operator">++</span>
	<span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token number">5</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">f3</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>y <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//1 返回值赋值  返回值为y   此时y=x=5</span>
<span class="token comment">// 2 执行defer 修改x的值 此时x=6,y的值没有改变</span>
<span class="token comment">// 3 真正的返回y 所以结果为5</span>
	x <span class="token operator">:=</span> <span class="token number">5</span>
	<span class="token keyword">defer</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		x<span class="token operator">++</span>
	<span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> x
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">f4</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>x <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//1 返回值赋值  返回值为x   此时x=5</span>
<span class="token comment">// 2 执行defer 修改局部变量x的值，而不是返回值x的值</span>
<span class="token comment">// 3 真正的返回  所以结果为5</span>
	<span class="token keyword">defer</span> <span class="token keyword">func</span><span class="token punctuation">(</span>x <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		x<span class="token operator">++</span>
	<span class="token punctuation">}</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token number">5</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">f1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">//5</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">f2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>    <span class="token comment">//6</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">f3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">//5</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">f4</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">//5</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br></div></div><h2 id="_8-内置函数介绍"><a href="#_8-内置函数介绍" class="header-anchor">#</a> 8 内置函数介绍</h2> <table><thead><tr><th>内置函数</th> <th>介绍</th></tr></thead> <tbody><tr><td>close</td> <td>主要用来关闭channel</td></tr> <tr><td>len</td> <td>用来求长度，比如string、array、slice、map、channel</td></tr> <tr><td>new</td> <td>用来分配内存，主要用来分配值类型，比如int、struct。返回的是指针</td></tr> <tr><td>make</td> <td>用来分配内存，主要用来分配引用类型，比如chan、map、slice</td></tr> <tr><td>append</td> <td>用来追加元素到数组、slice中</td></tr> <tr><td>panic和recover</td> <td>用来做错误处理 类似于java语言中的try-catch-finally</td></tr></tbody></table> <h3 id="_8-1-panic-recover"><a href="#_8-1-panic-recover" class="header-anchor">#</a> 8.1 panic/recover</h3> <blockquote><p>Go语言追求简洁优雅，所以，Go语言不支持传统的 <code>try…catch…finally</code> 这种异常，因为Go语言的设计者们认为，将异常与控制结构混在一起会很容易使得代码变得混乱。因为开发者很容易滥用异常，甚至一个小小的错误都抛出一个异常。在Go语言中，使用多值返回来返回错误。不要用异常代替错误，更不要用来控制流程。在极个别的情况下，才使用Go中引入的<code>Exception</code>处理：<code>defer</code>, <code>panic</code>, <code>recover</code>。</p></blockquote> <div class="custom-block tip"><p class="custom-block-title">panic</p> <ul><li>1、<code>panic</code>用于主动抛出错误,<code>panic</code>可以在任何地方引发，<code>panic</code>能够改变程序的控制流，当一个函数调用执行<code>panic</code>时，它会立刻停止执行函数中其他的代码，然后马上运行其中的<code>defer</code>函数，执行成功后会返回给调用方。</li> <li>2、假如函数F中书写了<code>panic</code>语句，会终止其后要执行的代码，在<code>panic</code>所在函数F内如果存在要执行的<code>defer</code>函数列表，按照<code>defer</code>的逆序执行</li> <li>3、返回函数F的调用者G,在G中,调用函数F语句之后的代码不会执行,假如函数G中存在要执行的<code>defer</code>函数列表,按照<code>defer</code>的逆序执行,这里的<code>defer</code>有点类似 <code>try-catch-finally</code> 中的 <code>finally</code></li> <li>4、直到<code>goroutine</code>整个退出，并报告错误</li></ul></div> <p><a data-fancybox="" title="exception" href="/img/goImage/exception.png"><img src="/img/goImage/exception.png" alt="exception"></a></p> <div class="custom-block tip"><p class="custom-block-title">recever</p> <ul><li>1、<code>recover</code> 用来捕获<code>panic</code> 抛出的错误。但<code>recover</code>只有在<code>defer</code>调用的函数中有效。</li> <li>2、用来控制一个<code>goroutine</code>的<code>panicking</code>行为，捕获<code>panic</code>，从而影响应用的行为</li> <li>3、在<code>defer</code>函数中,通过<code>recever</code>来终止一个<code>goroutine</code>的<code>panicking</code>过程,从而恢复正常代码的执行</li> <li>4、可以获取通过<code>panic</code>传递的<code>error</code></li></ul></div> <p>简单来讲：go中可以抛出一个<code>panic</code>的异常，然后在<code>defer</code>中通过<code>recover</code>捕获这个异常，然后正常处理。</p> <h3 id="_8-2-示例代码一"><a href="#_8-2-示例代码一" class="header-anchor">#</a> 8.2 示例代码一</h3> <p><strong>main函数相当于调用者G，f函数相当于函数F</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;c&quot;</span><span class="token punctuation">)</span>
   <span class="token keyword">defer</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 必须要先声明defer，否则不能捕获到panic异常</span>
      fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;d&quot;</span><span class="token punctuation">)</span>
      <span class="token keyword">if</span> err <span class="token operator">:=</span> <span class="token function">recover</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
         fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token comment">// 这里的err其实就是panic传入的内容</span>
      <span class="token punctuation">}</span>
      fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;e&quot;</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
   <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//开始调用f</span>
   fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;f&quot;</span><span class="token punctuation">)</span> <span class="token comment">//这里开始下面代码不会再执行</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">)</span>
   <span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">&quot;异常信息&quot;</span><span class="token punctuation">)</span>
   fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;b&quot;</span><span class="token punctuation">)</span> <span class="token comment">//这里开始下面代码不会再执行</span>
<span class="token punctuation">}</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>output<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
c
a
d
异常信息
e
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p><a data-fancybox="" title="error" href="/img/goImage/error.png"><img src="/img/goImage/error.png" alt="error"></a></p> <h3 id="_8-3-示例代码二"><a href="#_8-3-示例代码二" class="header-anchor">#</a> 8.3 示例代码二</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">funcA</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;func A&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">funcB</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">&quot;panic in B&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">funcC</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;func C&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">funcA</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token function">funcB</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token function">funcC</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>输出结果如下<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
<span class="token keyword">func</span> A
<span class="token builtin">panic</span><span class="token punctuation">:</span> <span class="token builtin">panic</span> in B

goroutine <span class="token number">1</span> <span class="token punctuation">[</span>running<span class="token punctuation">]</span><span class="token punctuation">:</span>
main<span class="token punctuation">.</span><span class="token function">funcB</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span>
        <span class="token operator">...</span><span class="token operator">/</span>code<span class="token operator">/</span><span class="token keyword">func</span><span class="token operator">/</span>main<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">12</span>
main<span class="token punctuation">.</span><span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token operator">...</span><span class="token operator">/</span>code<span class="token operator">/</span><span class="token keyword">func</span><span class="token operator">/</span>main<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">20</span> <span class="token operator">+</span><span class="token number">0x98</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>程序运行期间funcB中引发了panic导致程序崩溃，异常退出了。这个时候我们就可以通过recover将程序恢复回来，继续往后执行。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">funcA</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;func A&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">funcB</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">defer</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		err <span class="token operator">:=</span> <span class="token function">recover</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token comment">//如果程序出出现了panic错误,可以通过recover恢复过来</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;recover in B&quot;</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">&quot;panic in B&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">funcC</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;func C&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">funcA</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token function">funcB</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token function">funcC</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="_8-4-recover-panic注意点"><a href="#_8-4-recover-panic注意点" class="header-anchor">#</a> 8.4 recover/panic注意点</h3> <ul><li>利用recover处理panic指令，defer必须在panic之前声明，否则当panic时，recover无法捕获到panic．</li> <li>recover()必须搭配defer使用。</li> <li>defer一定要在可能引发panic的语句之前定义。</li></ul></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/learn-go/21 Go语言指针.html" class="prev">
        Go语言指针
      </a></span> <span class="next"><a href="/go/learn-go/23  fmt.Printf总结.html">
        fmt.Printf总结
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/76.61078c10.js" defer></script>
  </body>
</html>
